package sd.qd.xiaoming.coffeedesk.controller;

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import java.util.UUID;

import javax.servlet.http.HttpServletRequest;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.propertyeditors.CustomDateEditor;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.ServletRequestDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import sd.qd.xiaoming.coffeedesk.entity.CdFile;
import sd.qd.xiaoming.coffeedesk.entity.CdShareAccount;
import sd.qd.xiaoming.coffeedesk.service.CdFileService;
import sd.qd.xiaoming.coffeedesk.service.CdShareAccountService;

@Controller
@RequestMapping("/share_account")
public class CdShareAccountController {

    @Autowired
    private CdShareAccountService cdShareAccountService;
    @Autowired
    private CdFileService cdFileService;

    @ResponseBody
    @RequestMapping("/create_share_account/{id}")
    public Object createShareAccount(@PathVariable("id") final Long id,
            @RequestParam("deadTime") final Date deadTime) {
        CdShareAccount act = new CdShareAccount();
        act.setDeadTime(deadTime);
        act.setCode(UUID.randomUUID().toString());
        act.setAccessJson(id.toString());
        this.cdShareAccountService.save(act);
        Map<String, Object> result = new HashMap<String, Object>();
        result.put("code", act.getCode());
        return result;
    }

    @ResponseBody
    @RequestMapping("/get_share_file_id/{code}")
    public Object getShareAccountFileId(@PathVariable("code") final String code) {
        Map<String, Object> result = new HashMap<String, Object>();
        try {
            CdShareAccount account = this.cdShareAccountService.findByCode(code);
            if (account.getDeadTime().before(new Date())) {
                result.put("sucess", false);
                result.put("msg", "您使用的口令已经过期！");
                return result;
            }
            result.put("sucess", true);
            CdFile file = this.cdFileService.findById(Long.parseLong(account.getAccessJson()));
            file.setDirectory(null);
            result.put("file", file);
            return result;
        } catch (Exception e) {
            result.put("sucess", false);
            result.put("msg", "您输入的口令不正确！");
            return result;
        }
    }

    @InitBinder
    protected void initBinder(final HttpServletRequest request,
            final ServletRequestDataBinder binder) throws Exception {
            DateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            CustomDateEditor editor = new CustomDateEditor(df, false);
            binder.registerCustomEditor(Date.class, editor);
    }

}
